Ext.setup( {
	icon : 'icon.png',
	tabletStartupScreen : 'tablet_startup.png',
	phoneStartupScreen : 'phone_startup.png',
	glossOnIcon : false,
	onReady : function() {
		var form;
		
		 Ext.regModel('receivedocument', {
	       	    fields: [
	       	        {name: 'AGENCYID', type: 'string'},
	       	        {name: 'COMMITER', type: 'string'},
	    	        {name: 'DEPARTMENT', type: 'string'},
	    	        {name: 'TITLE', type: 'string'},
	    	        {name: 'CONTENT', type: 'string'},
	    	        {name: 'TODEPARTMENT', type: 'string'},
	    	        {name: 'DOCTYPENAME', type: 'string'},
	    	        {name: 'RESPONSIBLE', type: 'string'},
	    	        {name: 'EMERGENCY', type: 'string'},
	    	        {name: 'SECRET', type: 'string'},
	    	        {name: 'DESCRIBE', type: 'string'}
	       	    ],
	       	 validations: [ 
	       	               	  {type: 'presence', name: 'AGENCYID'},
				              {type: 'presence', name: 'TITLE'},
				              {type: 'presence', name: 'CONTENT'},
				              {type: 'presence', name: 'TODEPARTMENT'},
				              {type: 'presence', name: 'DOCTYPENAME'},
				              {type: 'presence', name: 'RESPONSIBLE'}
				          ]
	       	});
		
		
		  Ext.Ajax.request({
	             url: '/wipzIphone/dic/dic_userAndDepariment.action',
	             success: function(response, opts) {
	        	     var jsonObj = eval("(" + response.responseText + ")");
	        	     

	        	    	var user = Ext.ModelMgr.create({
	        	    		COMMITER  : jsonObj.COMMITER,
	        	    		DEPARTMENT : jsonObj.DEPARTMENT
	        	    	}, 'receivedocument');
	        	    
	        	    	form.load(user);
	             }
	         });
		  
		   Ext.regModel('agency', {
	      	    fields: [
	      	        {name: 'NAME', type: 'string'},
	      	        {name: 'CODE',  type: 'string'}
	      	    ]
	      	});

	        	var agencyStore = new Ext.data.Store({
	        		autoLoad: true,
	        	    model: 'agency',
	        	    proxy: {
	        	        type: 'ajax',
	        	        url : '/wipzIphone/dic/dic_workflowAgency.action',
	        	        reader: {
	        	            type: 'json'
	        	        }
	        	    }
	        	});
	        	
	        	   Ext.regModel('doctypedic', {
	             	    fields: [
	             	        {name: 'ID', type: 'string'},
	             	        {name: 'DOCTYPE', type: 'string'},
	             	        {name: 'CODE',  type: 'string'}
	             	    ]
	             	});
	        	
	        	var doctypeStore = new Ext.data.Store({
	        		autoLoad: true,
	        	    model: 'doctypedic',
	        	    proxy: {
	        	        type: 'ajax',
	        	        url : '/wipzIphone/dic/dic_documentType.action',
	        	        reader: {
	        	            type: 'json'
	        	        }
	        	    }
	        	});
	        	
		 Ext.regModel('agenymapmodel', {
       	    fields: [
       	        {name: 'AGENCYID', type: 'string'},
       	        {name: 'NAME', type: 'string'}
       	    ]
       	});
		 
		 Ext.regModel('useAgenDeptmodel', {
	       	    fields: [
	       	        {name: 'NAME', type: 'string'}
	       	    ]
	       	});
		 
			var useAgenDeptStore = new Ext.data.Store({
		  		autoLoad: true,
		  	    model: 'useAgenDeptmodel',
		  	    proxy: {
		  	        type: 'ajax',
		  	        url : '/wipzIphone/dic/dic_useAgenDept.action',
		  	        reader: {
		  	            type: 'json'
		  	        }
		  	    }
		  	});
		 
		 
		 
  	
  	var agenymapStore = new Ext.data.Store({
  		autoLoad: true,
  	    model: 'agenymapmodel',
  	    proxy: {
  	        type: 'ajax',
  	        url : '/wipzIphone/dic/dic_agencyMap.action',
  	        reader: {
  	            type: 'json'
  	        }
  	    }
  	});
		
		var formBase = {
			scroll : 'vertical',
			standardSubmit : false,
			items : [ {
				xtype : 'fieldset',
				title : '所属单位',
				items : [ {
					xtype : 'selectfield',
					name : 'AGENCYID',
					label : '单位名称',
					placeHolder : '请选择所属单位',
					valueField: 'AGENCYID',
					displayField:'NAME',
					store : agenymapStore,
					required : true
				} ]
			}, {
				xtype : 'fieldset',
				title : '基本信息',
				defaults : {
					required : true,
					labelAlign : 'left',
					labelWidth : '40%'
				},
				items : [ {
					xtype : 'textfield',
					name : 'COMMITER',
					label : '登记人',
					useClearIcon : false
				}, {
					xtype : 'textfield',
					name : 'DEPARTMENT',
					label : '来文单位',
					useClearIcon : false
				} ]
			}, {
				xtype : 'fieldset',
				title : '详细信息',
				defaults : {
					xtype : 'fieldset'
				},
				items : [  {
					xtype : 'textfield',
					name : 'TITLE',
					label : '收文标题',
					required : true
				},{
					xtype : 'textareafield',
					name : 'CONTENT',
					label : '内容',
					maxLength : 60,
					maxRows : 3,
					required : true
				}, {
					xtype : 'selectfield',
					name : 'TODEPARTMENT',
					label : '主办部门',
					store : agencyStore,
					valueField: 'NAME',
					displayField:'NAME',
					placeHolder : '请选择主办部门',
					required : true
				}, {
					xtype : 'selectfield',
					name : 'DOCTYPENAME',
					label : '收文类型',
					valueField: 'ID',
					displayField:'DOCTYPE',
					placeHolder : '请选择发文类型',
					store : doctypeStore,
					required : true
				},  {
					xtype : 'selectfield',
					name : 'RESPONSIBLE',
					label : '主办责任人',
					valueField: 'NAME',
					displayField:'NAME',
					placeHolder : '请选择主办责任人',
					store : useAgenDeptStore,
					required : true
				},{
					xtype : 'selectfield',
					name : 'EMERGENCY',
					label : '紧急程度',
					options : [  {
						text : '普通',
						value : '1'
					}, {
						text : '紧急',
						value : '2'
					}  ]
				}, {
					xtype : 'selectfield',
					name : 'SECRET',
					label : '密级',
					options : [  {
						text : '秘密',
						value : '1'
					}, {
						text : '机密',
						value : '2'
					}  ]
				}, {
					xtype : 'textareafield',
					name : 'DESCRIBE',
					label : '备注',
					maxLength : 60,
					maxRows : 3
				} ]
			} ],
			listeners : {
				submit : function(form, result) {
					console.log('success', Ext.toArray(arguments));
				},
				exception : function(form, result) {
					console.log('failure', Ext.toArray(arguments));
				}
			},

			dockedItems : [ {
				xtype : 'toolbar',
				dock : 'bottom',
				items : [ {
					xtype : 'spacer'
				}, {
					text : '取消',
					handler : function() {
					document['success'] = 'false';
					}
				}, {
					text : '提交',
					ui : 'confirm',
					handler : function() {
					 var currentNote = form.getRecord(); 
		             form.updateRecord(currentNote); 
	 
	                var errors = currentNote.validate(); 
	                if (!errors.isValid()) { 
	             Ext.Msg.alert('等待...', "带*号选项为必填信息！", Ext.emptyFn); 
	                    return; 
	                } 
					
						Ext.Msg.confirm("提示", "确认对以上信息进行提交吗？", function(a) {
							if(a+""=="yes"){
							form.submit( {
								waitMsg : {
									message : '正在提交...',
									cls : 'demos-loading'
								},
						     url: "/wipzIphone/document/receiveDocument_insert.action",           
							 method: 'POST',           
							 success: function(thisForm, action){ 
									Ext.Msg.alert('提示', '提交成功，等待下级审批人‘'+action.NEXTOPERATOR+'’审批！', function() {
										document['success'] = 'true';
									});
									unLoadMask(cmpMask);  
								
								}
							});
						}else {
							return;
						}
					}
						);
						}
				} ]
			} ]
		};

		if (Ext.is.Phone) {
			formBase.fullscreen = true;
		} else {
			Ext.apply(formBase, {
				autoRender : true,
				floating : true,
				modal : true,
				centered : true,
				hideOnMaskTap : false,
				height : 385,
				width : 480
			});
		}

		form = new Ext.form.FormPanel(formBase);
		form.show();
	}
});